package com.sinochem.yunlian.ship.base.biz;

import com.sinochem.yunlian.ship.api.vo.uc.UcUserInfo;
import com.sinochem.yunlian.ship.base.entity.BsFlickerScreen;
import com.sinochem.yunlian.ship.base.mapper.BsFlickerScreenMapper;
import com.sinochem.yunlian.ship.base.vo.request.FlickerActiveVo;
import com.sinochem.yunlian.ship.base.vo.request.FlickerScreenVo;
import com.sinochem.yunlian.ship.common.biz.BaseBiz;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import com.sinochem.yunlian.ship.common.util.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinochem.yunlian.ship.base.entity.BsFlickerActive;
import com.sinochem.yunlian.ship.base.mapper.BsFlickerActiveMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 闪屏活动表
 *
 * @author ship-admin
 * @email shipadmin@sinochem.com
 * @date 2018-05-19 09:43:29
 */
@Service
public class BsFlickerActiveBiz extends BaseBiz<BsFlickerActiveMapper,BsFlickerActive> {

    @Autowired
    private BsFlickerScreenBiz flickerScreenBiz;

    /**
     * 先删除再插入
     * @param vo
     * @return
     */
    //@Transactional(rollbackFor = Exception.class)
    public RestResponse addActive(FlickerActiveVo vo){

        //活动信息
        UcUserInfo ucUserInfo = vo.getUcUserInfo();
        BsFlickerActive flickerActive = new BsFlickerActive();
        flickerActive.setLinkUrl(vo.getLinkUrl());
        flickerActive.setDisplayTime(vo.getDisplayTime());
        flickerActive.setStartTime(DateUtils.parseDate(vo.getStartTime(),DateUtils.DEFAILT_DATE_PATTERN));
        flickerActive.setEndTime(DateUtils.parseDate(vo.getEndTime(),DateUtils.DEFAILT_DATE_PATTERN));
        flickerActive.setCreateBy(ucUserInfo.getId());
        flickerActive.setCreateTime(new Date());

        this.deleteActive(vo);
        this.insertSelective(flickerActive);


        //图片信息
        List<BsFlickerScreen> bsFlickerScreens = new ArrayList<>();
        List<FlickerScreenVo> imgUrls = vo.getImgUrls();
        if(imgUrls!=null && imgUrls.size()>0){
            for (FlickerScreenVo voImg:imgUrls) {
                BsFlickerScreen bsFlickerScreen = new BsFlickerScreen();
                BeanUtils.copyProperties(voImg,bsFlickerScreen);
                bsFlickerScreen.setActiveId(flickerActive.getId());
                bsFlickerScreens.add(bsFlickerScreen);
            }
        }
        flickerScreenBiz.flickerScreenAddList(bsFlickerScreens);
        return RestResponse.success();
    }

    /**
     * 删除
     * @param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public RestResponse deleteActive(){
        this.delete(null);
        flickerScreenBiz.delete(null);
        return RestResponse.success();
    }

    public void deleteActive(FlickerActiveVo vo){
        this.delete(null);
        flickerScreenBiz.delete(null);
    }

    /**
     * 详情查询
     * @param vo
     * @return
     */
    public RestResponse getActiveDetail(FlickerActiveVo vo){
        List<BsFlickerActive> flickerActives =  this.selectListAll();
        if(flickerActives==null || flickerActives.isEmpty()){
            return RestResponse.success(); //没有闪屏
        }
        BsFlickerActive flickerActive = flickerActives.get(0);
        List<BsFlickerScreen> bsFlickerScreens = flickerScreenBiz.getBsFlickerScreenListByActiveId(flickerActive.getId());
        List<FlickerScreenVo> imgUrls = new ArrayList<>();

        if(bsFlickerScreens!=null && bsFlickerScreens.size()>0){
            for (BsFlickerScreen bsFlickerScreen:bsFlickerScreens) {
                FlickerScreenVo voImg = new FlickerScreenVo();
                BeanUtils.copyProperties(bsFlickerScreen,voImg);
                imgUrls.add(voImg);
            }
        }

        FlickerActiveVo result = new FlickerActiveVo();
        result.setId(flickerActive.getId());
        result.setLinkUrl(flickerActive.getLinkUrl());
        result.setDisplayTime(flickerActive.getDisplayTime());
        result.setStartTime(DateUtils.formatDate(flickerActive.getStartTime(),DateUtils.DEFAILT_DATE_PATTERN));
        result.setEndTime(DateUtils.formatDate(flickerActive.getEndTime(),DateUtils.DEFAILT_DATE_PATTERN));
        result.setImgUrls(imgUrls);

        return new RestResponse(result);
    }

    /**
     * APP 闪屏查询
     * @param flickerScreenVo
     * @return
     */
    public RestResponse getBsFlickerActive(FlickerScreenVo flickerScreenVo){

        //图片
        BsFlickerScreen uniScreen = new BsFlickerScreen();
        uniScreen.setMobileTerminal(flickerScreenVo.getMobileTerminal());
        uniScreen.setDisplaySize(flickerScreenVo.getDisplaySize());
        BsFlickerScreen bsFlickerScreen = flickerScreenBiz.getBsFlickerScreenOrDefault(uniScreen);
        if(bsFlickerScreen==null || bsFlickerScreen.getActiveId()==null){
            return RestResponse.success();
        }

        //活动
        BsFlickerActive flickerActive = this.selectById(bsFlickerScreen.getActiveId());
        FlickerActiveVo result = new FlickerActiveVo();
        result.setId(flickerActive.getId());
        result.setLinkUrl(flickerActive.getLinkUrl());
        result.setDisplayTime(flickerActive.getDisplayTime());
        result.setStartTime(DateUtils.formatDate(flickerActive.getStartTime(),DateUtils.DEFAILT_DATE_PATTERN));
        result.setEndTime(DateUtils.formatDate(flickerActive.getEndTime(),DateUtils.DEFAILT_DATE_PATTERN));
        result.setImgUrl(bsFlickerScreen.getImgUrl());

        return new RestResponse(result);
    }
}